package com.example.project;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.project.Function.mainpage;
import com.example.project.Function.register.register;
import com.example.project.key.connection.databaseconn;
import com.example.project.key.object.employee.employee;
import com.example.project.key.service.employeeService.employeeService;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class MainActivity extends AppCompatActivity {

    private Button btn_log;
    private EditText ed_name;
    private EditText ed_pwd;
    private Button btn_reg;

    private static Connection con=null;
    private static PreparedStatement stmt = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        employeeService ems=new employeeService();
        if(!ems.getInfo(MainActivity.this).getUsername().isEmpty()){
            Intent login =new Intent(MainActivity.this, mainpage.class);
            startActivity(login);
        }else {
            setContentView(R.layout.activity_main);
            btn_log=findViewById(R.id.login);
            btn_reg=findViewById(R.id.register);
            ed_name=findViewById(R.id.lg_user);
            ed_pwd=findViewById(R.id.lg_pwd);
            btn_reg=findViewById(R.id.register);

            try {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        con = databaseconn.getConnection();
                    }
                }).start();
            }catch (Exception e)
            {
                e.printStackTrace();
            }

            btn_reg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent reg = new Intent(MainActivity.this, register.class);
                    startActivity(reg);
                }
            });

            btn_log.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(ed_pwd.getText().toString().isEmpty()||ed_name.getText().toString().isEmpty())
                    {
                        Toast.makeText(MainActivity.this,"注册信息不完整",Toast.LENGTH_LONG).show();
                    }else {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                employee emm = new employee();
                                if(ems.isRight(con,ed_name.getText().toString(),ed_pwd.getText().toString()))
                                {
                                    emm =ems.find(con,ed_name.getText().toString(),ed_pwd.getText().toString());
                                    ems.saveUserInfo(MainActivity.this,emm);
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                                            Intent login =new Intent(MainActivity.this, mainpage.class);
                                            startActivity(login);
                                        }
                                    });
                                }else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(MainActivity.this,"密码或账号错误",Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            }
                        }).start();
                    }
                }
            });
        }

        
    }
}